﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace zlh.Adminyemian
{
    public partial class adminxinxibianji : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void Page_PreInit(object sender, EventArgs e)
        {
            if (Session["color"] == "1")
            {
                Page.Theme = "1";

            }
            else if (Session["color"] == "2")
            {
                Page.Theme = "2";
            }
            else
            {
                Page.Theme = "3";
            }
        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            string strCn = "server=pc.hstc.cn;database=dhd2014112130;uid=2014112130;pwd=19970514";
            SqlConnection cn = new SqlConnection(strCn);

            try
            {
                cn.Open();
                string strsql = "select stuNO from StudentInformation ";
                SqlCommand cmd = new SqlCommand(strsql, cn);
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.Read())//该用户能读到
                {
                    this.lblInfo.Text = "该用户已经存在";//显示错误信息

                }
                else
                {
                    StringBuilder sb = new StringBuilder(1024);
                    sb.Append("INSERT INTO StudentInformation ");
                    sb.Append("                (stuNO, stuName, Gender, Age, College, Major, Class, Memo,Address) ");
                    sb.Append("VALUES   (@stuNO, @stuName, @Gender, @Age, @College,@Major, @Class, @Memo, @Address) ");

                    SqlParameter[] pms = new SqlParameter[9];
                    pms[0] = new SqlParameter("@stuNO", this.infoNo.Text);
                    pms[1] = new SqlParameter("@stuName", this.infoname.Text);
                    pms[2] = new SqlParameter("@Gender", infogender.Text);
                    pms[3] = new SqlParameter("@Age", this.infoage.Text);
                    pms[4] = new SqlParameter("@College", this.infocol.Text);
                    pms[5] = new SqlParameter("@Major", this.infomaj.Text);
                    pms[6] = new SqlParameter("@Class", this.infocla.Text);
                    pms[7] = new SqlParameter("@Memo", this.infomem.Text);
                    pms[8] = new SqlParameter("@Address", this.infoadd.Text);

                    int intResult = (int)SqlHelper.ExecuteNonQuery(sb.ToString(), pms);
                    this.lblInfo.Text = (intResult < 1) ? "新增失败！" : "新增" + intResult.ToString() + "条记录！";
                }
            }
            catch
            {
            }
        }

        protected void readbtn_Click(object sender, EventArgs e)
        {
            string strSql = "SELECT   stuNO, stuName, Gender, Age, College, Major, Class, Memo,Address FROM      StudentInformation WHERE   (stuNO = @stuNO)";
            SqlParameter[] pms = new SqlParameter[1];
            pms[0] = new SqlParameter("@stuNO", this.infoNo.Text);
            SqlDataReader dr = SqlHelper.ExecuteReader(strSql, pms);
            if (dr.Read())
            {
                this.infoname.Text = dr["stuName"].ToString();
                this.infogender.Text = dr["Gender"].ToString();
                this.infoage.Text = dr["Age"].ToString();
                this.infocol.Text = dr["College"].ToString();
                this.infomaj.Text = dr["Major"].ToString();
                this.infocla.Text = dr["Class"].ToString();
                this.infomem.Text = dr["Memo"].ToString();
                this.infoadd.Text = dr["Address"].ToString();
            }
            dr.Close();//关闭
        }

        protected void upbtn_Click(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder(1024);
            sb.Append("UPDATE  StudentInformation ");
            sb.Append("SET         stuNO = @stuNO, stuName = @stuName, Gender = @Gender, Age = @Age, College = @College, Major = @Major, Class = @Class,  ");
            sb.Append("                Memo = @Memo,Address = @Address ");
            sb.Append("WHERE   (stuNO = @stuNO) ");

            SqlParameter[] pms = new SqlParameter[9];
            pms[0] = new SqlParameter("@stuNO", this.infoNo.Text);
            pms[1] = new SqlParameter("@stuName", this.infoname.Text);
            pms[2] = new SqlParameter("@Gender", infogender.Text);
            pms[3] = new SqlParameter("@Age", this.infoage.Text);
            pms[4] = new SqlParameter("@College", this.infocol.Text);
            pms[5] = new SqlParameter("@Major", this.infomaj.Text);
            pms[6] = new SqlParameter("@Class", this.infocla.Text);
            pms[7] = new SqlParameter("@Memo", this.infomem.Text);
            pms[8] = new SqlParameter("@Address", this.infoadd.Text);

            int intResult = SqlHelper.ExecuteNonQuery(sb.ToString(), pms);
            this.lblInfo.Text = (intResult < 1) ? "更新失败！" : "更新" + intResult.ToString() + "条记录！";
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder(1024);
            sb.Append("DELETE FROM StudentInformation ");
            sb.Append("WHERE   (stuNO = @stuNO) ");

            SqlParameter[] pms = new SqlParameter[9];
            pms[0] = new SqlParameter("@stuNO", this.infoNo.Text);
            pms[1] = new SqlParameter("@stuName", this.infoname.Text);
            pms[2] = new SqlParameter("@Gender", infogender.Text);
            pms[3] = new SqlParameter("@Age", this.infoage.Text);
            pms[4] = new SqlParameter("@College", this.infocol.Text);
            pms[5] = new SqlParameter("@Major", this.infomaj.Text);
            pms[6] = new SqlParameter("@Class", this.infocla.Text);
            pms[7] = new SqlParameter("@Memo", this.infomem.Text);
            pms[8] = new SqlParameter("@Address", this.infoadd.Text);

            int intResult = (int)SqlHelper.ExecuteNonQuery(sb.ToString(), pms);
            this.lblInfo.Text = (intResult < 1) ? "删除失败！" : "删除" + intResult.ToString() + "条记录！";
        }

        
    }
}